<?php
/**
 * Created by PhpStorm.
 * User: LG
 * Date: 2017/8/30
 * Time: 13:50
 */

namespace app\admin\controller;

/*
 * 权限
 */
use think\Db;
use app\common\model\AdminRole;
class Role extends Base
{
    protected $role;
    protected $role_group = "admin_auth_group";
    public function __construct()
    {
        parent::__construct();
        $this->role = new AdminRole();
    }

    /**
     * 角色列表
     * @author  ligang
     * @request     string      title       GET     角色名称
     * @request     int         page        GET     页码
     */
    public function index(){

        //获取条件
        if (input('?get.title')){
            $where['title'] = ['like','%'.input('get.title').'%'];
        }else{
            $where = array();
        }
        //获取页码
        $Nowpage = input('get.page') ? input('get.page'):1;
        //获取数据
        $lists = $this->role->selectPage($Nowpage,$where);
        //模板输出
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $lists['page']['allpage']); //总页数
        $this->assign('count', $lists['page']['count']);
        //更改时间格式
        foreach ($lists['data'] as $key=>$value){
            $lists['data'][$key]['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
            $lists['data'][$key]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
        }
        $json = array();
        $json['data'] = $lists['data'];
        $json['page'] =  $lists['page'];
        if(input('get.page')){
            return json($json);
        }
        //渲染模板
        return $this->fetch('role/role_manage');
    }

    /**
     * 添加角色
     */
    public function addRole()
    {
        return $this->fetch('role/role_manage_adduser');
    }

    /**
     * 添加数据(修改)
     * @author  ligang
     * @request     string      title       GET     角色名称
     * @request     int         status      GET     角色状态
     * @request     int         id          GET     角色ID
     */
    public function doAddRole()
    {
        //获取角色名称、角色状态
        if (input('?get.title') && input('?get.status')){
            $title = input('get.title');
            $status = input('get.status/d');
            $data['title'] = $title;
            $data['status'] = $status;
            //若存在ID，本次操作为修改，反之则是新增
            if (input('?get.id')){
                $id = input('get.id/d');
                $data['id'] = $id;
                $data['update_time'] = time();
                //更新数据
                $this->role->updateData($data);
                return json(array('code'=>1,'msg'=>'修改成功'));
            }else{
                //判断角色名称是否存在
                $checkTitle = $this->role->findData(array('title'=>$title));
                if ($checkTitle){
                    return json(array('code'=>0,'msg'=>'角色名称已存在'));
                }
                $data['add_time'] = time();
                $this->role->insertData($data);
                return json(array('code'=>1,'msg'=>'添加成功'));
            }
        }
        return json(array('code'=>0,'msg'=>'缺少必要参数'));
    }

    /**
     * 修改角色
     * @author  ligang
     * @request     int         id          GET     角色ID
     */
    public function editRole()
    {
        //获取角色ID
        if (!input('?get.id')){
            return json(array('code'=>0,'msg'=>'缺少必要参数'));
        }
        //转换ID类型
        $id = input('get.id/d');
        //获取ID对应的数据
        $data = Db::name($this->role_group)->where('id',$id)->find();
        //模板输出
        $this->assign('data',$data);
        //模板渲染
        return $this->fetch('role/role_manage_adduser');
    }
}